from wordcloud import WordCloud
import matplotlib.pyplot as plt
import pandas as pd
import os


def generate_wordcloud_from_file(word_freq_path, font_path, output_image, width=800, height=600):
    """从词频文件生成词云"""
    word_freq_df = pd.read_csv(word_freq_path)
    word_weight = dict(zip(word_freq_df["Word"], word_freq_df["Weight"]))

    wc = WordCloud(
        font_path=font_path,
        width=width,
        height=height,
        background_color='white',
        max_words=200,
        min_font_size=8,
        collocations=False
    )
    wc.generate_from_frequencies(word_weight)

    plt.figure(figsize=(10, 8), dpi=300)
    plt.imshow(wc)
    plt.axis("off")
    plt.savefig(output_image, dpi=300, bbox_inches='tight')
    print(f"词云已生成：{output_image}")
    plt.close()


def get_font_path():
    """尝试查找可用的中文字体"""
    possible_fonts = [
        "/System/Library/Fonts/PingFang.ttc",
        "/System/Library/Fonts/STHeiti Light.ttc",
        "/Library/Fonts/SimHei.ttf"
    ]
    for font in possible_fonts:
        if os.path.exists(font):
            print(f"找到中文字体: {font}")
            return font
    print("警告: 未找到中文字体，词云可能无法正确显示中文")
    return None